package com.example.jmx.init;

import com.example.jmx.common.properties.MyProperties;
import com.sun.jdmk.comm.HtmlAdaptorServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import javax.management.MBeanServer;
import javax.management.ObjectName;
import java.lang.management.ManagementFactory;

/**
 * jmx修改properties的值
 */
@Component
public class MyPropertyRunner implements CommandLineRunner {

    @Autowired
    private MyProperties myProperties;

    @Override
    public void run(String... args) throws Exception {
        System.out.println("开始注册MBean...Properties");
        MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
        mBeanServer.registerMBean(myProperties, new ObjectName("com.example.jmx.common.properties:type=MyPropertiesMBean, name=myProperties"));
        //注册到http接口
        HtmlAdaptorServer html = new HtmlAdaptorServer();
        ObjectName adaptorName = new ObjectName("adapator:name=HtmlAdaptor2");
        html.setPort(9097);
        MBeanServer platformMBeanServer1 = ManagementFactory.getPlatformMBeanServer();
        platformMBeanServer1.registerMBean(html, adaptorName);
        html.start();
        System.out.println("注册MBean完毕");
    }
}
